<template>
    <section>
        <!-- 背景色 -->
        <div class="color"></div>
        <div class="color"></div>
        <div class="color"></div>
        <div class="box">
            <div class="circle" style="--x:0"></div>
            <div class="circle" style="--x:1"></div>
            <div class="circle" style="--x:2"></div>
            <div class="circle" style="--x:3"></div>
            <div class="circle" style="--x:4"></div>

            <el-steps :active="active"  finish-status="success">
                <el-step title="基本信息"></el-step>
                <el-step title="详情信息"></el-step>
                <el-step title="完成"></el-step>
            </el-steps>


            <!-- 注册框 -->
            <div class="container">
<!--      基本信息          -->
                <div class="form" v-if="active == 0">
                    <h2>注册</h2>
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                        <el-form-item  label="用户名" prop="username">
                            <el-input v-model="ruleForm.username"></el-input>
                        </el-form-item>
                        <el-form-item  label="uuid" prop="username" style="display: none">
                            <el-input v-model="ruleForm.uuid"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" prop="checkPass">
                            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="用户类型" prop="type">
                            <el-select v-model="ruleForm.type" placeholder="请选择用户类型">
                                <el-option label="患者" value="1"></el-option>
                                <el-option label="医生" value="2"></el-option>
                                <el-option label="医学生" value="3"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="邮箱" prop="email">
                        <el-input v-model="ruleForm.email"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button style="margin-top: 12px;" @click="submitForm('ruleForm')">下一步</el-button>
                            <el-button @click="resetForm('ruleForm')">重置</el-button>
                        </el-form-item>
                    </el-form>

                </div>
<!--      患者信息          -->
                <div class="form" v-if="active == 1&&ruleForm.type==1">
                    <h2>患者信息完善</h2>
                    <el-form :model="ruleForm1" :rules="rules" ref="ruleForm1" label-width="100px" class="demo-ruleForm">
                        <el-form-item  label="uuid" prop="userid" style="display: none">
                            <el-input v-model="ruleForm1.userid"></el-input>
                        </el-form-item>
                        <el-form-item  label="姓名" prop="name">
                            <el-input v-model="ruleForm1.name"></el-input>
                        </el-form-item>
                        <el-form-item label="年龄" prop="age">
                            <el-input  v-model.number="ruleForm1.age" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="身高(厘米)" prop="height">
                            <el-input  v-model.number="ruleForm1.height" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="体重（公斤）" prop="weight">
                            <el-input  v-model.number="ruleForm1.weight" autocomplete="off"></el-input>
                        </el-form-item>

                        <el-form-item label="糖尿病类型" prop="diabetictype">
                            <el-select v-model="ruleForm1.diabetictype" placeholder="请选择糖尿病类型">
                                <el-option v-for="dict in czyOptions"
                                           :key="dict.id"
                                           :label="dict.diabetesname"
                                           :value="dict.diabetesname"></el-option>

                            </el-select>
                        </el-form-item>
                        <el-form-item label="症状" prop="symptom">
                            <el-select v-model="ruleForm1.symptom" placeholder="请选择症状" multiple
                                       clearable :style="{ width: '270px' }">
                                <el-option v-for="dict in symptomOptions"
                                           :key="dict.id"
                                           :label="dict.symptom"
                                           :value="dict.symptom"></el-option>

                            </el-select>
                        </el-form-item>
                        <el-form-item label="过往病史">
                            <el-input type="textarea" v-model="ruleForm1.pasthistory"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button style="margin-top: 12px;" @click="submitForm1('ruleForm1')">下一步</el-button>
                            <el-button @click="resetForm('ruleForm')">重置</el-button>
                        </el-form-item>
                    </el-form>

                </div>
<!--      医生信息          -->
                <div class="form" v-if="active == 1&&ruleForm.type!=1">
                    <h2>医生类信息完善</h2>
                    <el-form :model="ruleForm2" :rules="rules" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
                        <el-form-item  label="uuid" prop="userid" style="display: none">
                            <el-input v-model="ruleForm2.userid"></el-input>
                        </el-form-item>
                        <el-form-item  label="姓名" prop="name">
                            <el-input v-model="ruleForm2.name"></el-input>
                        </el-form-item>
                        <el-form-item label="年龄" prop="age">
                            <el-input  v-model.number="ruleForm2.age"></el-input>
                        </el-form-item>
                        <el-form-item label="毕业院校" prop="graduate">
                            <el-input  v-model="ruleForm2.graduate" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="就职医院" prop="hospital">
                            <el-input  v-model="ruleForm2.hospital" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="专业" prop="education">
                            <el-input  v-model="ruleForm2.education" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="医龄" prop="years">
                            <el-input  v-model.number="ruleForm2.years" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="自我介绍">
                            <el-input type="textarea" v-model="ruleForm2.self"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button style="margin-top: 12px;" @click="submitForm2('ruleForm2')">下一步</el-button>
                            <el-button @click="resetForm('ruleForm2')">重置</el-button>
                        </el-form-item>
                    </el-form>

                </div>
                <div class="form" v-if="active == 2">

                    <h3>恭喜你完成所有信息注册，前往
                        <router-link to="/login" style="text-decoration: none">登录页面</router-link>
                    </h3>

                </div>
            </div>
<!--            <el-button style="margin-top: 12px;" @click="next">下一步</el-button>-->
        </div>



    </section>
</template>

<script>
    import { uuid } from 'vue-uuid';
    export default {
        data() {
            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('年龄不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value > 100) {
                            callback(new Error('请输入正常年龄'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            var checkWeight = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('体重不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value > 300||value<1) {
                            callback(new Error('请输入正常体重'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            var checkHeight = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('身高不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value > 250||value<76) {
                            callback(new Error('请输入正常身高'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            var checkYears = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('医龄不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value > 70) {
                            callback(new Error('请输入正常医龄'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                active: 0,
                czyOptions: [],
                symptomOptions:[],
                ruleForm: {
                    username:'',
                    type:'',
                    password: '',
                    checkPass: '',
                    email: '',
                    uid:'',
                },
                ruleForm1: {
                    name: '',
                    age: '',
                    weight:'',
                    height:'',
                    diabetictype:[],
                    symptom:[],
                    pasthistory:'',
                    userid:''
                },
                ruleForm2: {
                    name: '',
                    age: '',
                    hospital:'',
                    graduate:'',
                    education:'',
                    years:'',
                    self:'',
                    userid:''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
                    ],
                    type: [
                        { required: true, message: '请选择用户类型', trigger: 'change' },

                    ],
                    password: [
                        { validator: validatePass, trigger: 'blur' },
                        { min:3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                    ],
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' },
                        { min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'blur' }
                    ],
                    age: [
                        { validator: checkAge, trigger: 'blur' }
                    ],
                    weight: [
                        { validator: checkWeight, trigger: 'blur' }
                    ],
                    symptom: [
                        { required: true, message: '请选择症状', trigger: 'change' },

                    ],
                    diabetictype: [
                        { required: true, message: '请选择糖尿病类型', trigger: 'change' },

                    ],
                    height: [
                        { validator: checkHeight, trigger: 'blur' }
                    ],
                    years: [
                        { validator: checkYears, trigger: 'blur' }
                    ],

                }
            };
        },

        methods: {
            next() {
                if (this.active++ > 2) this.active = 0;
            },
            submitForm(formName) {
                console.log(formName);
                const _this = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post("http://localhost:8181/register/add",this.ruleForm).then(function (resp) {
                            if (resp.data.msg == 'success'){
                                _this.$message("注册成功");
                                if (_this.active++ > 2) _this.active = 0;
                            }else {
                                _this.$message("你的用户名已被注册");
                            }

                        });

                    } else {

                        return false;
                    }
                });
            },
            submitForm1(formName) {
                const _this = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post("http://localhost:8181/patient/add",this.ruleForm1).then(function (resp) {
                            if (resp.data.msg == 'success'){
                                _this.$message("完善成功！");
                                if (_this.active++ > 2) _this.active = 0;
                            }else {
                                _this.$message("还有资料错误！");
                            }

                        });

                    } else {

                        return false;
                    }
                });
            },
            submitForm2(formName) {
                const _this = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post("http://localhost:8181/doctor/add",this.ruleForm2).then(function (resp) {
                            if (resp.data.msg == 'success'){
                                _this.$message("完善成功！");
                                if (_this.active++ > 2) _this.active = 0;
                            }else {
                                _this.$message("还有资料错误！");
                            }

                        });

                    } else {

                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },


        },
        created() {
            const _this = this;
            axios.get("http://localhost:8181/listdiaType").then(function (resp) {
                _this.czyOptions = resp.data.data;
            });
            axios.get("http://localhost:8181/listSymptom").then(function (resp) {
                _this.symptomOptions = resp.data.data;
            });
            _this.ruleForm.uuid = uuid.v1();
            _this.ruleForm1.userid = _this.ruleForm.uuid;
            _this.ruleForm2.userid = _this.ruleForm.uuid;
        },



    }
</script>

<style src="../../assets/css/register.css" scoped>

</style>